<?php
class User
{
    
    
    //会员导入
    
    
    
    
    static function get_area_name_by_addr($addr)
    {
        $model = new Model();
        $obj = $model->table("area")->where("name='{$addr}'")->find();
        if(empty($obj))
        {
            return '';
        }
        else
        {
            return $obj['id'];
        }
    }
    
    static function get_addr_code_by_str($jb, $str)
    {
        $arr = str_split($str,'9');
    
        if($jb === 1)//县
        {
            $addr1 = $arr[0];
            $addr2  = $arr[1];
            $addr3  = $arr[2];
            return self::get_area_name_by_addr($addr1).",".self::get_area_name_by_addr($addr2).",".self::get_area_name_by_addr($addr3);
        }
        elseif($jb === 2)//市
        {
            $addr1 = $arr[0];
            $addr2  = $arr[1];
            return self::get_area_name_by_addr($addr1).",".self::get_area_name_by_addr($addr2);
        }
    }
    
    static function insert_old_user_to_new()
    {
        header('content-type:text/html;charset=utf-8');
        set_time_limit(0);
        $model = new Model();
        //jb 1县代理  2市级代理
        $old_user_list = $model->table("member")->select();
    
        for($i=0,$c=count($old_user_list); $i<$c; $i++)
        {
            $data = $mx = [];
            $model->query("start transaction");
            $id  = intval( $old_user_list[$i]['id']);
            $jb = intval( $old_user_list[$i]['proxy_jb'] );
            $addr = $old_user_list[$i]['proxy_dz'];
    
            $validcode = CHash::random(8);
            //一级密码
            $passWord = $old_user_list[$i]['upass'];
            //二级密码
            $passwordtwo = $old_user_list[$i]['pass2'];

            $data['id'] = $id;

            //直推人数
            $data['u_tjrs']      = $old_user_list[$i]['spread_num'];
            $data['name']        = $old_user_list[$i]['uname'];
            $data['pass1']       = $passWord;
            $data['pass2']       = $passwordtwo;
            $data['password']    = CHash::md5($passWord,$validcode);
            $data['passwordtwo'] = CHash::md5($passwordtwo);
            $data['validcode']   = $validcode;



            //推荐人id

            $data['tjr_id']      = $old_user_list[$i]['pid'];
            //print_r($data);die;
    
            $mx['real_name']     = $old_user_list[$i]['name'];
            $mx['mobile']        = $old_user_list[$i]['mobile'];
            //注册时间
            $mx['reg_time']      = date('Y-m-d H:i:s',$old_user_list[$i]['created_at']);

            if($jb === 1)
            {
                $mx['group_id'] = 3;//县级
                $mx['gwjf'] = 20000;
            }
            elseif($jb === 2)
            {
                $mx['group_id'] = 4;//市级
                $mx['gwjf'] = 50000;
            }
            else
            {
                $member = intval($old_user_list[$i]['vip']);
                if($member === 3)//  腾飞1级代理
                {
                    $mx['group_id'] = 1;
                    $mx['gwjf'] = 2000;
                }
                elseif($member === 2)// 腾飞2级代理
                {
                    $mx['group_id'] = 2;
                    $mx['gwjf'] = 5000;
                }
            }
    
            if(!empty($addr) && $id !== 151)
            {
                $mx['dl_area'] = self::get_addr_code_by_str($jb, $addr);
            }
            if($id === 151)
            {
                $mx['dl_area'] = self::get_area_name_by_addr('河北省').",".self::get_area_name_by_addr('石家庄市');
            }
            $nobj = Common::get_user_obj_by_name($data['name']);
            if(empty($nobj))
            {
                $res1 = $model->table("user")->data($data)->insert();
            }
            
    
            if($res1)
            {
                $mx['user_id'] = $res1;
                $res2 = $model->table("customer")->data($mx)->insert();
    
                $model->query("commit");
                 
            }
            else
            {
                $model->query("rollback");
                continue;
            }
    
        }
    }
    
    
}
?>